package com.zd.jdbc;

import com.zd.bean.ShoppingStore;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ShoppingStoreDAOImpl implements ShoppingStoreDAO {
    private static final String URL = "jdbc:mysql://hadoop102:3306/zhengzhou_tourism_business?serverTimezone=Asia/Shanghai";
    private static final String USER = "root";
    private static final String PASSWORD = "000000";

    @Override
    public void insert(ShoppingStore store) {
        // 严格按表结构顺序: store_name, region_name, category, address, contact_phone, status, create_time, update_time
        String sql = "INSERT INTO shopping_stores (store_name, region_name, category, address, contact_phone, status, create_time, update_time) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, store.getStoreName());
            pstmt.setString(2, store.getRegionName());
            pstmt.setString(3, store.getCategory());
            pstmt.setString(4, store.getAddress());  // 地址字段在contact_phone之前
            pstmt.setString(5, store.getContactPhone());
            pstmt.setInt(6, store.getStatus());
            pstmt.setTimestamp(7, Timestamp.valueOf(store.getCreateTime()));
            pstmt.setTimestamp(8, Timestamp.valueOf(store.getUpdateTime()));

            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Integer> getAllStoreIds() {
        List<Integer> storeIds = new ArrayList<>();
        String sql = "SELECT store_id FROM shopping_stores";

        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                storeIds.add(rs.getInt("store_id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return storeIds;
    }
}